package com.ronri.ams.sample;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.cz.nongyetongv5.R;
import com.ronri.ams.SystemEnvironmental;

/**
 * 文件：activity_control_edit_box.java
 * 包名：com.ronri.ams.sample;   --- 正式发布时需要修正
 * 说明：编辑器盒子
 * 作者：MS During
 * 日期：2017/5/12
 * 历史：
 */
public class activity_control_edit_box extends abstract_activity {

    private Context mContext = null;

    // 获取编辑控件
    private EditText mEditView = null;

    // 获取语音按钮控件
    private Button mbtnVoice = null;

    // 获取定位按钮控件
    private Button mbtnLocate =null;

    // 获取条码
    private Button mbtnBarcode = null;

    // 返回结果
    private int mResultCode = 0;

    // 结果
    private String mValue = "";

    /***
     * 创建视图
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("aaa","activity_control_edit_box");
        // 读取参数
        Bundle bundle = getIntent().getExtras();

        mResultCode = bundle.getInt("resultCode", 0);
        mValue = bundle.getString("edit_value", "");

        int editLayout = bundle.getInt("edit_layout", R.layout.control_edit_box);
        String szTitle = bundle.getString("title");
        String szvTitle = bundle.getString("vtitle");

        // 设置视图
        setContentView(editLayout);

        mContext = this;

        // 显示标题与右边功能
        setTitle(szTitle, szvTitle);

        // 初始化内容
        init();

        // 读取控件
        mEditView = (EditText)findViewById(R.id.controlEdit);
        mbtnVoice = (Button)findViewById(R.id.buttonGetVoice);
        mbtnLocate= (Button)findViewById(R.id.buttonGetLocation);
        mbtnBarcode = (Button)findViewById(R.id.buttonGetBarCode);

        // 设置值
        if(mEditView != null){
            mEditView.setText(mValue);
            mEditView.setSelection(mValue.length());

//            if(bundle.getBoolean("onlyNumber",false)){
//                mEditView.setInputType(InputType.TYPE_CLASS_NUMBER);
//            }
        }

        /***
         * 获取定位信息
         */
        if (mbtnLocate != null) {
            mbtnLocate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getLoacteAddressText();
                }
            });
        }

        if(mbtnBarcode != null){
            mbtnBarcode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, activity_barcode_scanning.class);
                    startActivityForResult(intent, SystemEnvironmental.Sample_Barcode_Scanning);
                }
            });
        }

        /***
         * 获取语音信息
         */
        if (mbtnVoice != null){
            mbtnVoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getVoiceText();
                }
            });
        }
    }

    /***
     * 通过内部控制设置内容
     * @param szString
     */
    @Override
    public void CallBackResult(String szString) {
        super.CallBackResult(szString);
        if (mEditView != null){
            mEditView.setText(szString);
            mEditView.setSelection(mValue.length());
        }
    }

    /***
     * 确定返回结果
     * @return
     */
    @Override
    public boolean SureReturn() {

        //关闭软键盘
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }

        mValue = mEditView.getText().toString();

        Intent intent = new Intent();

        Bundle bundle = new Bundle();

        bundle.putString("returnValue", mValue);

        intent.putExtras(bundle);

        // 设置返回结果
        setResult(RESULT_OK, intent);

        return super.SureReturn();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK){
            if (requestCode == SystemEnvironmental.Sample_Barcode_Scanning){
                if (data != null){
                    Bundle bundle = data.getExtras();
                    String szString = bundle.getString("result");
                    mEditView.setText(szString);
                }
            }
        }
    }
}

